package com.community.common.util;

import com.community.common.pojo.dto.PTDto;
import com.community.common.pojo.dto.PTRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Slf4j
@Component
public class RequestUtil {

    private static RestTemplate rest;

    @Resource
    private RestTemplate restTemplate;


    @PostConstruct
    public void init() {
        rest = restTemplate;
    }

    public static String post(String url, Object reportData) {
        String result = null;
        try {
            result = rest.postForObject(url, reportData, String.class);
        } catch (Exception e) {
            log.error("调用接口异常！URL 为 " + url, e);
        }
        return result;
    }

    public static PTRes postForPt(String url, PTDto ptDto) {
        PTRes result = null;
        try {
            result = rest.postForObject(url, ptDto, PTRes.class);
        } catch (Exception e) {
            log.error("调用接口异常！URL 为 " + url, e);
        }
        return result;
    }

}
